/**
 * 
 */
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.IssueTypeBO;
import org.cuatrovientos.erp.model.IssueType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;



/**
 * @author Daniel Lacasta
 */
@Controller
public class IssueTypeController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(IssueTypeController.class);

	@Autowired
    private IssueTypeBO issueTypeBO;
    
        /**
         * default constructor
         */
        public IssueTypeController () {
               logger.info("IssueType BEAN instantiated");
                
        }
        
        
        
        /**
         * handles default /issueType or /issueType/list request
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/issueType","/issueType/list"})
         */
        @RequestMapping({"/issueType","/issueType/list"})
        public ModelAndView issueTypeDefault() {
        	logger.info("default");
 
                Map<String, Object> model = new HashMap<String, Object>();

                List<IssueType> issueTypeList = issueTypeBO.selectAll();
                model.put("issueTypes", issueTypeList );
                return new ModelAndView("IssueTypeList", model);     
        }



        /**
         * handles delete issueType, and redirect to issueType default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/issueType","/issueType/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/issueType/delete"})
        public String deleteIssueType(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("delete " + id);

            issueTypeBO.deleteById(id);
            return "redirect:/issueType";

        }
        
        /**
         * handles new issueType request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it: in this case "issueType"
         * @return the name of the view to show
         * @RequestMapping({"/issueType/new"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/issueType/new"})
        public ModelAndView newIssueType() {
        		logger.info("new issueType ");
                IssueType issueType = new IssueType();
               
                return new ModelAndView("NewIssueType", "issueType", issueType);
        }
        
        /**
         * handles new issueType request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/issueType/new"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/issueType/save"})
        public String newIssueType(@Valid IssueType issueType, BindingResult bindingResult) {
        		logger.info("new issueType " + issueType.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("issueType", issueType);
        			return "NewIssueType";
        		}
        		
        		// If data is ok, insert and go on
                issueTypeBO.insert(issueType);
                return "redirect:/issueType";
        }
        
        /**
         * handles delete issueType, and redirect to issueType default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/issueType","/issueType/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/issueType/detail"})
        public ModelAndView issueTypeDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("Detail " + id);
            Map<String,Object> model = new HashMap<String,Object>();
            Object issueType = issueTypeBO.selectById(id);
            model.put("issueType", issueType);
            
            return new ModelAndView("IssueTypeDetail",model);

        }
        
        /**
         * handles issueType update, first part: register loaded
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/issueType/update"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/issueType/update"})
        public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("update " + id);
                Map<String,Object> model = new HashMap<String,Object>();
                Object issueType = issueTypeBO.selectById(id);
                model.put("issueType", issueType);
               return new ModelAndView("UpdateIssueType",model);

        }
        
        /**
         * handles save update issueType request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/issueType/saveUpdate"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/issueType/saveupdate"})
        public String saveUpdate(@Valid IssueType issueType, BindingResult bindingResult) {
        		logger.info("update issueType " + issueType.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("issueType", issueType);
        			return "UpdateIssueType";
        		}
        		
                issueTypeBO.update(issueType);
                return "redirect:/issueType";
        }
}
